const path = require("path")
const { VueLoaderPlugin } = require("vue-loader")
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin")
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const HtmlWebpackPlugin = require("html-webpack-plugin")

module.exports = {
  devtool: 'eval-source-map',
  entry: path.join(__dirname, "/src/main.js"),
  output: {
    path: path.join(__dirname, "/dist/images/"),
    filename: "bundle.js",
  },
  resolve: {
    alias: {
      vue: "@vue/runtime-dom",
      "@": path.join(__dirname, "src"),
    },
    extensions: ['.js', '.jsx', '.json']
  },
  module: {
    rules: [{
      test: /\.js$/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env'],
          plugins: ['@babel/plugin-proposal-class-properties'],
        }
      },
      exclude: /node_modules/
    },
    {
      test: /\.vue$/,
      use: [{
        loader: "vue-loader",
      },],
    },
    {
      test: /\.(sa|sc|c)ss$/,
      use: [{
        loader: MiniCssExtractPlugin.loader,
      },
        "css-loader",
        "postcss-loader",
        "sass-loader",
      {
        loader: "sass-resources-loader",
        options: {
          resources: path.resolve(__dirname, "./src/styles/_variables.scss")
        }
      }
      ],
    },
    {
      test: /\.(png|jpe?g|gif|svg|woff|eot|ttf)(\?.*)?$/,
      type: 'asset/resource',
      parser: {
        dataUrlCondition: {
          maxSize: 8 * 1024, //小于 8kb 的文件，将会视为 inline 模块类型，否则会被视为 resource 模块类型。
        },
      },
      generator: {
        filename: '[base]',
      },
    },
    ],
  },
  optimization: {
    minimizer: [new OptimizeCssAssetsPlugin()],
  },
  plugins: [
    new VueLoaderPlugin(),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css",
    }),
    new HtmlWebpackPlugin({
      template: path.join(__dirname, "./src/index.html"),
      filename: process.env.NODE_ENV === "production" ? "../index.html" : "index.html",
      minify: {
        // 压缩html
        removeComments: true, // 去除注释
        collapseWhitespace: false, // 压缩空白
      },
    }),
  ],
  devServer: {
    compress: true,
    port: 4000,
    host: "0.0.0.0",
  },
}